package mulis;

import divasoft.client.forms.FUserEditor;
import dvsdk.client.MulisComboBoxModelUsers;
import dvsdk.db.BeanUser;
import dvsdk.GlobalConfig;
import dvsdk.form.IInternal;
import dvsdk.util.Lang;
import dvsdk.util.Util;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Ivan
 */
public class LoginFrame extends javax.swing.JFrame implements IInternal{

    //private IFrameCore core = null;
    private boolean login = false;
    
    private int w=630;
    private int h=230;
    private int hM=290;
    private int hDB=90;
    

    /** Creates new form LoginFrame */
    public LoginFrame() {
        initComponents();
    }

    LoginFrame(java.awt.Frame parent, boolean modal) {
        //super(parent, ModalityType.TOOLKIT_MODAL);
        initComponents();
        jTextField1.setText(GlobalConfig.LOCATION);
        setAdminMode();
        checkLocPatch();
        this.setSize(w, h);
        this.setLocationRelativeTo(null);
        initUserList();
        GlobalConfig.getCore().checkUpdate(this);
        
        //jProgressBar1.setVisible(false);
    }

    public final void initUserList() {
        initUserList(false);
    } 
    
    public final void initUserList(boolean force) {
        jComboBox1.removeAllItems();
        if (jComboBox1.getModel() instanceof DefaultComboBoxModel) {
            ((DefaultComboBoxModel) jComboBox1.getModel()).removeAllElements();
        }
        if (jComboBox1.getModel() instanceof MulisComboBoxModelUsers) {
            ((MulisComboBoxModelUsers) jComboBox1.getModel()).removeAllElements();
        }
        if (GlobalConfig.LIST_USERS == null) {
            jComboBox1.setMaximumRowCount(0);
        } else if (!GlobalConfig.LIST_USERS.isEmpty()) {
            jComboBox1.setModel(new MulisComboBoxModelUsers(GlobalConfig.LIST_USERS));
            try {
                BeanUser user = ((MulisComboBoxModelUsers) jComboBox1.getModel()).getCurrentUserElement(jComboBox1.getSelectedIndex()); 
                if (force) { jComboBox1.setSelectedIndex(0); } else {jComboBox1.setSelectedIndex(Integer.parseInt(user.getIdr())-1);}
            } catch (Exception ex) {
                
            }
        }
        jPasswordField1.requestFocusInWindow();
    }

    public boolean test() {
        if (jComboBox1.getSelectedIndex() == -1) {
            return false;
        }
        if (((MulisComboBoxModelUsers) jComboBox1.getModel()).test(jComboBox1.getSelectedIndex(), jPasswordField1.getText())) {
            BeanUser user = ((MulisComboBoxModelUsers) jComboBox1.getModel()).getCurrentUserElement(jComboBox1.getSelectedIndex());
            GlobalConfig.AGENT_INN = user.getIdr();
            GlobalConfig.AGENT_NAME = user.getName();
            GlobalConfig.saveConfigToFile();
            GlobalConfig.getCore().refreshCurrentTable(true);
            GlobalConfig.getCore().setFullEnable(true);
            GlobalConfig.getCore().setEnable();
            return true;
        }
        return false;
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        initUserList();
        if (!GlobalConfig.warring.isEmpty()) {
            jLabel2.setText(GlobalConfig.warring);
            jLabel2.setForeground(Color.RED);
        }
        GlobalConfig.getCore().setFullEnable(false);
        GlobalConfig.getCore().setDisable();
    }

    private void setAdminMode() {
        jPanel2.setVisible(jToggleButton1.isSelected());
        GlobalConfig.IS_MASTER = jToggleButton1.isSelected();
        if (GlobalConfig.IS_MASTER) {
            this.setSize(w, hM);
        } else {
            this.setSize(w, h);
        }
    }

    private boolean checkLocPatch() {
        try {
            new File(GlobalConfig.LOCATION).mkdirs();
            if (!new File(GlobalConfig.LOCATION).exists() && !new File(GlobalConfig.LOCATION).canRead() && !new File(GlobalConfig.LOCATION).canWrite()) {
                jToggleButton1.setSelected(true);
                jPanel3.setVisible(false);
                jButton3.setVisible(false);
                jButton4.setVisible(false);
                jButton6.setVisible(false);
                this.setSize(w, hDB);
                JOptionPane.showMessageDialog(this, "База данных не доступна для чтения/записи.\nУкажите другой путь.", "Важно!", JOptionPane.WARNING_MESSAGE);
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
        return true;
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        gradientPanel1 = new gradientpanel.GradientPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mulis.MulisApp.class).getContext().getResourceMap(LoginFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        gradientPanel1.setColorEnd(resourceMap.getColor("gradientPanel1.colorEnd")); // NOI18N
        gradientPanel1.setColorStart(resourceMap.getColor("gradientPanel1.colorStart")); // NOI18N
        gradientPanel1.setDirection(3);
        gradientPanel1.setPreferredSize(new java.awt.Dimension(507, 4));

        javax.swing.GroupLayout gradientPanel1Layout = new javax.swing.GroupLayout(gradientPanel1);
        gradientPanel1.setLayout(gradientPanel1Layout);
        gradientPanel1Layout.setHorizontalGroup(
            gradientPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 613, Short.MAX_VALUE)
        );
        gradientPanel1Layout.setVerticalGroup(
            gradientPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 4, Short.MAX_VALUE)
        );

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel6.setForeground(resourceMap.getColor("jLabel6.foreground")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setToolTipText(resourceMap.getString("jLabel6.toolTipText")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel6MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(gradientPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(gradientPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel3.setName("jPanel3"); // NOI18N

        jComboBox1.setName("jComboBox1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jToggleButton1.setMnemonic('a');
        jToggleButton1.setText(resourceMap.getString("jToggleButton1.text")); // NOI18N
        jToggleButton1.setName("jToggleButton1"); // NOI18N
        jToggleButton1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jToggleButton1StateChanged(evt);
            }
        });
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jPasswordField1.setText(resourceMap.getString("jPasswordField1.text")); // NOI18N
        jPasswordField1.setName("jPasswordField1"); // NOI18N
        jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPasswordField1KeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, 391, Short.MAX_VALUE))
                        .addGap(58, 58, 58))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jToggleButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 302, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jToggleButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setName("jPanel2"); // NOI18N

        jButton4.setMnemonic('e');
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton3.setMnemonic('n');
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jButton5.setMnemonic('o');
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jTextField1.setEditable(false);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        jButton6.setMnemonic('e');
        jButton6.setText(resourceMap.getString("jButton6.text")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton6)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        login = test();
        if (login) {
            this.dispose();
            if (GlobalConfig.DEBUG > 0) {
                System.out.println("Login - OK");
            }
        } else {
            JOptionPane.showMessageDialog(this, "Неверный пароль", "Ошибка", JOptionPane.ERROR_MESSAGE);
            if (GlobalConfig.DEBUG > 0) {
                System.out.println("Login - NO");
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        GlobalConfig.getCore().exit();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (login) {
            GlobalConfig.getCore().setEnable();
        } else {
            GlobalConfig.getCore().exit();
        }
    }//GEN-LAST:event_formWindowClosing

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //String id=((MulisComboBoxModelUsers)jComboBox1.getModel()).getCurrentElement(jComboBox1.getSelectedIndex());
        FUserEditor fUserEditor = new FUserEditor(this);
        fUserEditor.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        String id = ((MulisComboBoxModelUsers) jComboBox1.getModel()).getCurrentElement(jComboBox1.getSelectedIndex());
        FUserEditor fUserEditor = new FUserEditor(id, this);
        fUserEditor.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jToggleButton1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jToggleButton1StateChanged
        setAdminMode();
    }//GEN-LAST:event_jToggleButton1StateChanged

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        JFileChooser fc = Lang.setUpdateUI(new JFileChooser(new File(GlobalConfig.LOCATION)));
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            //System.out.println(fc.getSelectedFile().getParentFile().getParent().toString() + "/bin/db/s_kvart.db");
            if (new File(fc.getSelectedFile().toString() + "/db/s_kvart.db").exists()) {
                JOptionPane.showMessageDialog(this, "Файлы с базой найдены!\nНеобходимо перезапустить программу.");
                GlobalConfig.LOCATION = fc.getSelectedFile().toString();
            } else {
                if (new File(fc.getSelectedFile().toString() + "/bin/db/s_kvart.db").exists() ||
                        new File(fc.getSelectedFile().toString() + "/bin/").exists()) {
                    if (JOptionPane.showConfirmDialog(this, "Была найдена поддиректория bin, использовать ее?", "Уточнение", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
                        GlobalConfig.LOCATION = fc.getSelectedFile().toString() + "/bin/";
                    } else {
                        return;
                    }
                }
                else if (new File(fc.getSelectedFile().getParentFile().getParent().toString() + "/bin/db/s_kvart.db").exists() ||
                        new File(fc.getSelectedFile().getParentFile().getParent().toString() + "/bin/").exists()) {
                    if (JOptionPane.showConfirmDialog(this, "Была найдена директория bin, использовать ее?", "Уточнение", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
                        GlobalConfig.LOCATION = fc.getSelectedFile().getParentFile().getParent().toString() + "/bin/";
                    } else {
                        return;
                    }
                } else if (JOptionPane.showConfirmDialog(this, "Файлы с базой не найдены. Создать новую базу?", "Создание новой базы", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
                    GlobalConfig.LOCATION = fc.getSelectedFile().toString();
                } else {
                    return;
                }
            }
            GlobalConfig.LOCATION=GlobalConfig.LOCATION.replace("/\\", "/").replace("\\/", "/").replace("//", "/");
            //GlobalConfig.LOCATION = fc.getSelectedFile().toString();
            GlobalConfig.saveConfigToFile();
            //JOptionPane.showMessageDialog(this, "Необходимо перезапустить программу!", "Важно!", JOptionPane.WARNING_MESSAGE);
            Util.restartProgram();
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        if (jTextField1.isEditable() && evt.getKeyCode() == KeyEvent.VK_ENTER) {
            GlobalConfig.LOCATION=jTextField1.getText().trim();
            GlobalConfig.saveConfigToFile();
            Util.restartProgram();
        }
        
        if (evt.getKeyCode() == KeyEvent.VK_ENTER && evt.getModifiersEx()==KeyEvent.ALT_DOWN_MASK) {
            jTextField1.setEditable(!jTextField1.isEditable());
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jPasswordField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordField1KeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            jButton1ActionPerformed(null);
        }
    }//GEN-LAST:event_jPasswordField1KeyPressed

    private void jLabel6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseClicked
        Util.runTeamViwerDL();
    }//GEN-LAST:event_jLabel6MouseClicked

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        String id = ((MulisComboBoxModelUsers) jComboBox1.getModel()).getCurrentElement(jComboBox1.getSelectedIndex());
        FUserEditor fUserEditor = new FUserEditor(id, this);
        String curNameUser=fUserEditor.getUserName();
        if (JOptionPane.showConfirmDialog(this, "Удалить пользователя ["+curNameUser+"] ?", "Вопрос", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE)==JOptionPane.YES_OPTION) {
            String[] users=fUserEditor.getUsers(id);
            String to_user=(String)JOptionPane.showInputDialog(this, "Назначить все объявления пользователю:", "Вопрос", JOptionPane.QUESTION_MESSAGE, null,users,users[0]);
            if (to_user!=null && !to_user.isEmpty())
                fUserEditor.delUser(to_user);
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private gradientpanel.GradientPanel gradientPanel1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables

    private Color origStart;
    private Color origEnd;
    private int colorStep;
    //private int colorCurrentStep;
    @Override
    public void showProgressBas(int max) {
        origStart=gradientPanel1.getColorStart();
        origEnd=gradientPanel1.getColorEnd();
        //colorCurrentStep=0;
        colorStep=origEnd.getRGB()/(max);
        
        //jProgressBar1.setValue(0);
        //gradientPanel1.
        //jProgressBar1.setMaximum(max);
        //jProgressBar1.setVisible(true);
        //gradientPanel1.setColorStart(Color.white);
        gradientPanel1.setColorEnd(new Color(colorStep));
        gradientPanel1.repaint();
        jButton1.setEnabled(false);
    }

    @Override
    public void hideProgressBas() {
        //jProgressBar1.setVisible(false);
        gradientPanel1.setColorEnd(origEnd);
        gradientPanel1.setColorStart(origStart);
        gradientPanel1.repaint();
        jButton1.setEnabled(true);
    }

    @Override
    public void resetProgressBas() {
        //jProgressBar1.setValue(0);
    }

    @Override
    public void incProgressBas() {
        //jProgressBar1.setValue(jProgressBar1.getValue()+1);
        //gradientPanel1.setColorStart(Color.white);
        gradientPanel1.setColorEnd(new Color(gradientPanel1.getColorEnd().getRGB()+colorStep));
        gradientPanel1.repaint();
    }
}
